@isTest
private class ContractLinkTest {
    static testMethod void testMethod1() {
       RecordType rt = [select Id from RecordType where SObjectType = 'Account' and DeveloperName = 'TerminalCustomer'];
        RecordType rtopp = [select Id from RecordType where SObjectType = 'Opportunity' and DeveloperName = 'CustomerOpportunity'];

        ProductStructure__c ps1 = new ProductStructure__c();
        ps1.Classify3__c = '仪器';
        
         ProductStructure__c ps2 = new ProductStructure__c();
        ps2.Classify3__c = '试剂';
        
        insert ps1;
        insert ps2;
        
        Product__c p1 = new Product__c();
        p1.Name = 'test product';
        p1.Category0__c = '仪器';
        p1.ProductStructure__c = ps1.Id;
        p1.Purpose__c = '仪器报价用';

        Product__c p2 = new Product__c();
        p2.Name = 'test product';
        p2.Category0__c = '试剂';
        p2.ProductStructure__c = ps2.Id;
        p2.Purpose__c = '试剂报价用';
        insert new Product__c[] {p1, p2};

        Province__c pro = TestUtils.createProvince();
        insert pro;

        City__c c = TestUtils.createCity();
        c.Province__c = pro.Id;
        insert c;

        District__c d = TestUtils.createDistrict();
        d.City__c = c.Id;
        insert d;

        // 客户
        Account acc = new Account();
        acc.Name = 'test customer';
        acc.Status__c = '审核通过';
        acc.RecordTypeId = rt.Id;
        acc.Phone = '123456';
        acc.Category__c = '综合医院';
        acc.Level__c = '一级医院';
        acc.CustomerAttribute__c = '公立医疗机构';
        acc.Address__c = '123456';
        acc.Province__c = pro.Id;
        acc.City__c = c.Id;
        acc.District__c = d.Id;
        acc.Postcode__c = '610000';
        acc.BedNumAuthorize__c = 100;
        acc.BedNumOpen__c = 100;
        acc.SpecialDepart__c = '内科_呼吸内科';
        acc.LastYearRevenue__c = 5000;
        insert acc;

        // 联系人补充
        Contact con1 = TestUtils.createContact();
        con1.LastName = '测试采购科';
        con1.AccountId = acc.Id;
        insert con1;

        Contact con2 = TestUtils.createContact();
        con2.LastName = '测试分管院长';
        con2.Post__c = '分管院长(设备)';
        con2.Departments__c = '医院办公室';
        con2.AccountId = acc.Id;
        insert con2;

        Contact con3 = TestUtils.createContact();
        con3.LastName = '测试财务';
        con3.Post__c = '科长/主任';
        con3.Departments__c = '财务科/计财处';
        con3.AccountId = acc.Id;
        insert con3;

        Contact con4 = TestUtils.createContact();
        con4.LastName = '测试检验';
        con4.Post__c = '科长/主任';
        con4.Departments__c = '检验科';
        con4.AccountId = acc.Id;
        insert con4;
        Account acc1 = TestUtils.createAccount();
        acc1.name = '迈克实业测试';
        acc1.Province__c = pro.Id;
        acc1.City__c = c.Id;
        acc1.District__c = d.Id;
        acc1.Level__c = '一级医院';

        acc1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Maccura','Account');
        insert acc1;
        // 业务机会
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        opp.StageCanChange__c = '意向';
        opp.StageName = '意向';
        opp.Department__c = '';
        opp.ExecuteCompany__c = acc1.Id;
        insert opp;
      
        // 意向产品
        IntentProduct__c ip = TestUtils.createIntentProduct();
        ip.Opportunity__c = opp.Id;
        ip.Product__c = p2.Id ; 
        insert ip;

        // 科室上年度收入
        Department__c dept = [select Id from Department__c where TerminalCustomer__c = :acc.Id];
        dept.LastYearRevenue__c = 1000;
        dept.PurchasingCost__c = 100;
        update dept;

        opp.StageName = '立项';
        opp.Department__c = '检验科';
        opp.ExecutiveCompany__c = acc1.Id;

        update opp;

        opp.StageCanChange__c = '立项';
       
        update opp;

        // 报价
        Quote q1 = new Quote();
        q1.Name = 'quote1';
        q1.OpportunityId = opp.Id;
        q1.IsActive__c = true;
        Quote q2 = new Quote();
        q2.OpportunityId = opp.Id;
        q2.Name = 'quote2';
        insert new Quote[] {q1, q2};

        // 报价产品
        QuoteProduct__c qp = new QuoteProduct__c(
            Quote__c = q1.Id,
            Product__c = p1.Id
        );
        insert qp;
                                            System.Test.startTest();

        // 测算
        Measure_Calculate__c mc = new Measure_Calculate__c(
             Opportunity__c = opp.Id,
            Status__c = '审批成功',
            IsActive__c  = true
        );
        insert mc;

        // 测算产品
        Measure_Calculate_Product__c mcp = new Measure_Calculate_Product__c(
            Measure_Calculate__c = mc.Id,
            Product__c = p2.Id  
        );
        insert mcp;
        q2.ActiveMeasure__c = mc.Id;
        update q2;
        opp.ActiveQuote__c = q1.Id;
        opp.ActiveMeasure__c = mc.Id;
        opp.StageName = '方案谈判';
        update opp;

        opp.StageCanChange__c = '方案谈判';
        update opp;
        opp.StageName = '招标';
        opp.PurchaseRoot__c = '公开招标';
        update opp;

        opp.StageCanChange__c = '招标';
        update opp;

        Contract__c con = new Contract__c(
            Account__c = acc.Id,
            TerminalCustomer__c = acc.Id

        );
        insert con;


        // ---------------------勾选画面测试-----------------------
        PageReference page = new PageReference('/apex/ContractLinkOpp?conid=' + con.Id);
        System.Test.setCurrentPage(page);
        ContractLinkOppController co = new ContractLinkOppController(new ApexPages.StandardController(new Contract__c()));

        co.init();

        System.assertEquals(2, co.qmbList.size());

        co.qmbList[0].chk = true;
        co.qmbList[1].chk = true;
        co.qmbList[1].isAppendRegent = '是';

        co.save();
        
        List<ContractProduct__c> cplist = [select Id from ContractProduct__c where Contract__c = :con.Id];
        System.assertEquals(2, cplist.size());

        // ---------------------明细画面测试-----------------------
        page = new PageReference('/apex/ContractLinkDetail?id=' + q1.id + '&t=q');
        System.Test.setCurrentPage(page);
        ContractLinkDetailController cd1 = new ContractLinkDetailController();
        cd1.init();

        page = new PageReference('/apex/ContractLinkDetail?id=' + mc.id + '&t=m');
        System.Test.setCurrentPage(page);
        ContractLinkDetailController cd2 = new ContractLinkDetailController();
        cd2.init();

        // ---------------------合同审批通过测试-----------------------
        ContractValidWebService.setContractValid(con.Id);
       
        Test.stopTest();
    }
}